/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import model.Film;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Matthy & Jelle
 */
public class FilmService extends AbstractService {

  @Override
  public boolean validate(Object o) {
    Film film = (Film) o;
    if (film.getFilmMaatschappij() == null) {
      return false;
    }
    if (film.getTitel() == null || film.getTitel().isEmpty()) {
      return false;
    }
    return true;
  }

  public Film getFilmByName(String filmnaam) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    Query query = session.createQuery("from Film f where f.titel = :titel");
    query.setString("titel", filmnaam);
    Film f = (Film) query.uniqueResult();
    session.getTransaction().commit();
    return f;
  }

  public Film getFilmById(int filmId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    Query query = session.createQuery("from Film f where f.id = :filmid");
    query.setInteger("filmid", filmId);
    Film f = (Film) query.uniqueResult();
    session.getTransaction().commit();
    return f;
  }
}
